package com.example.administrator.eventach;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;

//TouchView text
public class TouchView extends View implements GestureDetector.OnGestureListener, GestureDetector.OnDoubleTapListener {
    private Bitmap bitmap;
    private Matrix matrix;
    private float lastDis;
    private float lastDegrees;
    private GestureDetector gestureDetector;
    private boolean isDouble ;
   //构造方法
    public TouchView(Context context) {
        this(context, null);
    }

    public TouchView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TouchView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.image);
        matrix = new Matrix();
        gestureDetector = new GestureDetector(context,this);
        gestureDetector.setOnDoubleTapListener(this);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawBitmap(bitmap, matrix, null);
    }
 //旋转 、位移、侧滑、
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getPointerCount() == 2) {
            isDouble = true;
            float x1 = event.getX(0);
            float y1 = event.getY(0);
            float x2 = event.getX(1);
            float y2 = event.getY(1);
            float cx = (x1 + x2) / 2;
            float cy = (y1 + y2) / 2;
            float dis = (float) Math.sqrt(Math.pow(x1 - x2 , 2) + Math.pow((y1 - y2), 2));
            float degress = (float) Math.toDegrees(Math.atan2(x1-x2,y2-y1));
            switch (event.getActionMasked()) {
                case MotionEvent.ACTION_DOWN:
                case MotionEvent.ACTION_POINTER_DOWN:
                    lastDis = dis;
                    lastDegrees = degress;
                    break;
                case MotionEvent.ACTION_MOVE:
                    matrix.postScale(dis / lastDis, dis / lastDis, cx, cy);
                    matrix.postRotate(degress - lastDegrees, cx, cy);
                    lastDis = dis;
                    lastDegrees = degress;
                    break;
            }
        }else if(event.getPointerCount()==1){
            if(event.getAction()== MotionEvent.ACTION_DOWN){
                isDouble = false;
            }
            if(!isDouble){
                return gestureDetector.onTouchEvent(event);
            }
        }
        invalidate();
        return isDouble;
    }

    @Override
    public boolean onDown(MotionEvent motionEvent) {

        return true;
    }

    @Override
    public void onShowPress(MotionEvent motionEvent) {

    }

    @Override
    public boolean onSingleTapUp(MotionEvent motionEvent) {
        return false;
    }

    @Override
    public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent1, float distanceX, float distanceY) {
        matrix.postTranslate(-distanceX,-distanceY);
        invalidate();
        return false;
    }

    @Override
    public void onLongPress(MotionEvent motionEvent) {

    }

    @Override
    public boolean onFling(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
        return false;
    }

    @Override
    public boolean onSingleTapConfirmed(MotionEvent motionEvent) {
        return false;
    }

    @Override
    public boolean onDoubleTap(MotionEvent motionEvent) {
        return false;
    }

    @Override
    public boolean onDoubleTapEvent(MotionEvent e) {
        matrix.postScale(1.2f,1.2f,e.getX(),e.getY());
        invalidate();
        return false;
    }
}
